<?php
/**
 * 合同模块model
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/9 0009
 * Time: 下午 2:35
 */

namespace App\Model\Backstage;

use App\Model\Base;
use App\Model\Account\Type;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class Contract extends Base
{

    protected $table;
    public function __construct()
    {
        parent::__construct();
        $this->table = 'contract_'.$this->branch_id;
//        $this->table = 'contract';
    }



    /** 查询列表
     * @param $map
     * @return mixed
     */
    public function getList($map=[])
    {
        $list = DB::table($this->table)
                    ->leftJoin('user as ua', 'signatory', '=', 'ua.u_id')
                    ->leftJoin('user as ub', 'signatory_director', '=', 'ub.u_id')
                    ->leftJoin('department as da', 'signatory_department', '=', 'da.id')
                    ->leftJoin('department as db', 'signatory_director_department', '=', 'db.id')
                    ->where('c_status',0)
                    ->where('c_company_id',$this->store_id)
                    ->where($map)
                    ->select($this->table.'.*', 'ua.u_username as signatoryName', 'ub.u_username as signatory_departmentName',
                        'da.name as signatory_directorName', 'db.name as signatory_director_departmentName')
                    ->orderBy($this->table.'.id', 'desc')
                    ->paginate(env('PAGE_LIMIT_ADMIN'));
        if (sizeof($list) == 0) {
            return false;
        }
        return $list;
    }

    /**  添加
     * @param $data
     * @return mixed
     */
    public function add($data)
    {
        $data['created_at'] = time();
        $data['updated_at'] = time();
        $data['c_status'] = 0;//合同状态 0正常 1删除
        $data['c_company_id'] = $this->store_id;//公司ID
        $number = DB::table($this->table)->where('number',$data['number'])->value('number');
        if ($number) {
            return -1;
        }
        $cid =  DB::table($this->table)->insertGetId($data);
        if($cid){
            $log = new Log();
            $content = '添加合同：合同编号 => '.$data['number'];
            $log->addlog($this->store_id,$this->uid,11,$content);
            return true;
        }
        return false;



    }

    /** 修改
     * @param $data
     */
    public function edit($data)
    {
        $id = $data['id'];
        $oldInfo = $this->getData($id);
        $data['updated_at'] = time();
        unset($data['c_status']);
        unset($data['c_company_id']);
        unset($data['id']);
         unset($data['number']);
        //dd($data);
        $result =  DB::table($this->table)
            ->where('id', $id)
            ->update($data);
        if($result){
            $content = '修改合同：合同编号 => '.$oldInfo->number;
            if($oldInfo->type != $data['type']){
                $content .= ' 交易类型: '.$oldInfo->type.' => '.$data['type'];
            }
            if($oldInfo->address != $data['address']){
                $content .= ' 房源地址: '.$oldInfo->address.' => '.$data['address'];
            }
            if($oldInfo->price != $data['price']){
                $content .= ' 价钱(总价): '.$oldInfo->price.' => '.$data['price'];
            }
//            if($oldInfo->unit_price != $data['unit_price']){
//                $content .= ' 单价: '.$oldInfo->unit_price.' => '.$data['unit_price'];
//            }
            if($oldInfo->owner_name != $data['owner_name']){
                $content .= ' 业主姓名: '.$oldInfo->owner_name.' => '.$data['owner_name'];
            }
            if($oldInfo->owner_phone != $data['owner_phone']){
                $content .= ' 业主手机: '.$oldInfo->owner_phone.' => '.$data['owner_phone'];
            }
            if($oldInfo->owner_id_number != $data['owner_id_number']){
                $content .= ' 业主身份证号: '.$oldInfo->owner_id_number.' => '.$data['owner_id_number'];
            }
            if($oldInfo->owner_address != $data['owner_address']){
                $content .= ' 业主联系地址: '.$oldInfo->owner_address.' => '.$data['owner_address'];
            }
//            if($oldInfo->house_number != $data['house_number']){
//                $content .= ' 房产证号: '.$oldInfo->house_number.' => '.$data['house_number'];
//            }
            if($oldInfo->purpose != $data['purpose']){
                $content .= ' 用途: '.$oldInfo->purpose.' => '.$data['purpose'];
            }
            if($oldInfo->area != $data['area']){
                $content .= ' 产证面积: '.$oldInfo->area.' => '.$data['area'];
            }
            if($oldInfo->status != $data['status']){
                $content .= ' 状态: '.$oldInfo->status.' => '.$data['status'];
            }
            if($oldInfo->signatory != $data['signatory']){
                $content .= ' 签约人: '.$oldInfo->signatory.' => '.$data['signatory'];
            }
            if($oldInfo->signatory_department != $data['signatory_department']){
                $content .= ' 签约人部门: '.$oldInfo->signatory_department.' => '.$data['signatory_department'];
            }
            if($oldInfo->customer_name != $data['customer_name']){
                $content .= ' 客户姓名: '.$oldInfo->customer_name.' => '.$data['customer_name'];
            }
            if($oldInfo->customer_phone != $data['customer_phone']){
                $content .= ' 客户手机: '.$oldInfo->customer_phone.' => '.$data['customer_phone'];
            }
            if($oldInfo->customer_address != $data['customer_address']){
                $content .= ' 客户联系地址: '.$oldInfo->customer_address.' => '.$data['customer_address'];
            }
            if($oldInfo->customer_id_number != $data['customer_id_number']){
                $content .= ' 客户身份证号: '.$oldInfo->customer_id_number.' => '.$data['customer_id_number'];
            }
//            if($oldInfo->additional != $data['additional']){
//                $content .= ' 附加条款: '.$oldInfo->additional.' => '.$data['additional'];
//            }
//            if($oldInfo->remark != $data['remark']){
//                $content .= ' 备注: '.$oldInfo->remark.' => '.$data['remark'];
//            }
//            if($oldInfo->first_trial != $data['first_trial']){
//                $content .= ' 初审意见: '.$oldInfo->first_trial.' => '.$data['first_trial'];
//            }
//            if($oldInfo->review_case != $data['review_case']){
//                $content .= ' 复审意见: '.$oldInfo->review_case.' => '.$data['review_case'];
//            }
            if($oldInfo->c_contract_time != $data['c_contract_time']){
                $content .= ' 签约日期: '.$oldInfo->c_contract_time.' => '.$data['c_contract_time'];
            }
//            if($oldInfo->c_cate != $data['c_cate']){
//                $content .= ' 中介类别: '.$oldInfo->c_cate.' => '.$data['c_cate'];
//            }
            $log = new Log();
            $log->addlog($this->store_id,$this->uid,11,$content);
        }
        return $result;
    }

    /** 软删除
     * @param $id
     * @return mixed
     */
    public function deletes($id)
    {
        $oldStatus = DB::table($this->table)->where('id',$id)->value('c_status');
        $number = DB::table($this->table)->where('id',$id)->value('number');
        if($oldStatus == 0){
            //删除操作
            $result = DB::table($this->table)->where('id',$id)->update([
                'c_status' => 1,
                'updated_at' => time(),
                'c_del_department'=>$this->store_id,
                'c_del_user'    => $this->uid
            ]);
            if($result){
                $log = new Log();
                $content = '删除合同：合同编号 => '.$number;
                $log->addlog($this->store_id,$this->uid,11,$content);
            }
            return $result;
        }elseif($oldStatus == 1){
            //恢复操作
            $result = DB::table($this->table)->where('id',$id)->update([
                'c_status' => 0,
                'updated_at' => time(),
                'c_del_department'=>'',
                'c_del_user'    => ''
            ]);
            if($result){
                $log = new Log();
                $content = '恢复合同：合同编号 => '.$number;
                $log->addlog($this->store_id,$this->uid,11,$content);
            }
            return $result;
        }else{
            return -1;
        }

    }

    public function getData($id){
       return  DB::table($this->table)->where('id',$id)->select('number','type','address','price','unit_price','owner_name','owner_phone',
            'owner_id_number','owner_address','house_number','purpose','area','status','signatory','signatory_department',
            'customer_name','customer_phone','customer_address','customer_id_number','additional','remark','first_trial',
            'review_case','c_contract_time','c_cate')->first();
    }

    public function recycleBinList(){

    }
}